
#include "stdafx.h"

TEST(setsockoptTest, setsockopt_in_inout)
{
    SOCKET sock = INVALID_SOCKET;
    int optVal = 0xf0f0;
    int len = sizeof(optVal);
    
    Header header(GetCurrentThreadId(), "setsockopt");
    setsockopt_in_inout setsockoptIN(sock, SOL_SOCKET, SO_BROADCAST, 
                                     (char*) &optVal, len);
    setsockopt_in_inout setsockoptOUT;

    std::vector<char> buffer;
    Serialize(header, setsockoptIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(setsockoptOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (setsockoptIN.s_, setsockoptOUT.s_);
    EXPECT_EQ (setsockoptIN.level_, setsockoptOUT.level_);
    EXPECT_EQ (setsockoptIN.optname_, setsockoptOUT.optname_);
    if (setsockoptIN.optval_ && setsockoptOUT.optval_)
    {
        int cmp = memcmp (setsockoptIN.optval_, setsockoptOUT.optval_,
                          setsockoptIN.optlen_);
        EXPECT_TRUE (!cmp);
    }
    else {
        EXPECT_FALSE (setsockoptIN.optval_);
        EXPECT_FALSE (setsockoptOUT.optval_);
    }
    EXPECT_EQ (setsockoptIN.optlen_, setsockoptOUT.optlen_);
}

TEST(setsockoptTest, setsockopt_in_inout_NULL_parameters)
{
    SOCKET sock = INVALID_SOCKET;
    int optVal = 0xf0f0;
    int len = sizeof(optVal);
    
    Header header(GetCurrentThreadId(), "setsockopt");
    setsockopt_in_inout setsockoptIN(sock, SOL_SOCKET, SO_BROADCAST, 
                                     NULL, len);
    setsockopt_in_inout setsockoptOUT;

    std::vector<char> buffer;
    Serialize(header, setsockoptIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(setsockoptOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (setsockoptIN.s_, setsockoptOUT.s_);
    EXPECT_EQ (setsockoptIN.level_, setsockoptOUT.level_);
    EXPECT_EQ (setsockoptIN.optname_, setsockoptOUT.optname_);
    if (setsockoptIN.optval_ && setsockoptOUT.optval_)
    {
        int cmp = memcmp (setsockoptIN.optval_, setsockoptOUT.optval_,
                          setsockoptIN.optlen_);
        EXPECT_TRUE (!cmp);
    }
    else {
        EXPECT_FALSE (setsockoptIN.optval_);
        EXPECT_FALSE (setsockoptOUT.optval_);
    }
    EXPECT_EQ (setsockoptIN.optlen_, setsockoptOUT.optlen_);
}

TEST(setsockoptTest, setsockopt_out_inout)
{
    Header header(GetCurrentThreadId(), "setsockopt");
    setsockopt_out_inout setsockoptIN(7);
    setsockopt_out_inout setsockoptOUT;

    std::vector<char> buffer;
    Serialize(header, setsockoptIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(setsockoptOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (setsockoptIN.wsa_last_error_, setsockoptOUT.wsa_last_error_);
}

//TEST(setsockoptTest, setsockopt_out_inout_NULL_parameters)
//{
//    
//}